/*
*日历插件
*/
var lin_calendar={
	defaults:{
		//日历ID
		id:'',				
		//插入指定位置
		insertPosition:'',
		//是否显示功能按钮
		showBtn:true,
		//是否特殊显示节假日
		holiday:true,
		//已经预订的时间段
		orders:null,
		//日历个数
		count:1,
		//指定年份
		year:'',
		//指定月份
		month:'',
		//选择的日期
		selectDay:"",
		//当前回调函数
		onToday:'',
		//日历创建完毕回调函数
		onComplete:'',
		//选择日期回调函数				
		onSelectDay:''  		
	},
	init:function(config){
		var that=this;
		var config = $.extend(that.defaults,config);
		
		//日历对象列表
		that.aCal = [];
		//插入指定位置
		that.insertPosition =config.insertPosition || 'body';
		//日历id
		that.id = config.id || "lin_calendar_" + (+new Date());
		//日历内容容器
		that.container = document.createElement("div");
		that.container.id = this.id;
		//关闭按钮
		that.oClose = document.createElement("span");
		that.oClose.className = "lin_calendar_close";
		//左按钮
		that.oPrev = document.createElement("span");
		that.oPrev.className = "lin_calendar_prev";
		//右按钮
		that.oNext = document.createElement("span");
		that.oNext.className = "lin_calendar_next";
		//是否显示功能按钮
		that.bShowBtn = config.showBtn;
		//是否特殊显示节假日
		that.holiday = config.holiday;
		//已经预订的时间段
		that.orders = config.orders;		
		//日历个数
		that.count = config.count || 1;
		//指定年份
		that.Year = config.year || new Date().getFullYear();
		//指定月份
		that.Month = config.month || new Date().getMonth() + 1;
		//选择的日期
		that.selectDay = config.selectDay;
		//当天回调函数
		that.onToday = config.onToday || function() {};
		//日历创建完毕回调函数
		that.onComplete = config.onComplete || function() {};
		//选择日期回调函数
		that.onSelectDay = config.onSelectDay || function() {};
		//今天
		that.today = new Date().getFullYear() + "-" + that.format(new Date().getMonth() + 1) + "-" + that.format(new Date().getDate());
		
		//配置功能按钮
		that.setBtn(this.bShowBtn);
		//添加事件
		that.addEvent();	
	},
	
	//创建日历算法
	_Draw: function(iYear, iMonth)
	{	
		var that=this;
		var oContainer = document.createElement("div"),
		oDl = document.createElement("dl"),
		oDd = document.createElement("dd"),
		oFrag = document.createDocumentFragment(),
		//计算当月第一天是星期几
		firstDay = new Date(iYear, iMonth - 1, 1).getDay(),
		//计算当月有多少天
		lastDay = new Date(iYear, iMonth, 0).getDate(),
		//日历头
		aTmp = [
			"<dt class=\"date\">"+ iYear + "年" + iMonth +"月</dt>",
			"<dt><strong>日</strong></dt>",
			"<dt>一</dt>",
			"<dt>二</dt>",
			"<dt>三</dt>",
			"<dt>四</dt>",
			"<dt>五</dt>",
			"<dt><strong>六</strong></dt>"
		],
		arr = [],cur, oA, i, len, sValue, classIndex;
		for(i = 1; i <= firstDay; i++) arr.push(0);
		for(i = 1; i <= lastDay; i++) arr.push(i);
		while(arr.length)
		{
			for(i = 1, len = arr.length; i <= len; i++)
			{
				if(arr.length)
				{
					oA = document.createElement("a");
					sValue = arr.shift();
					if(!sValue)
					{
						oA.innerHTML = " ";
						oA.className = "disabled";
					}
					else
						{
							oA["data-date"] = iYear + "-" + this.format(iMonth) + "-" + that.format(sValue);
							oA["data-week"] = that.getWeek(oA["data-date"]);
							oA.href = "javascript:void(0);";
							oA.innerHTML = sValue;
							cur = new Date(iYear, iMonth - 1, sValue);
							//屏蔽今天以前的日期选择
							parseInt(oA["data-date"].replace(/-/g, "")) < parseInt(that.today.replace(/-/g, "")) && (oA.className = "disabled");
							//节假日处理
							if(that.holiday) for(var className in that.dayName) that.isHoliday(oA, className);
							//订单时间处理
							if(that.orders!=null)that.isHaveOrder(oA);
						}
				}
				oFrag.appendChild(oA)
			}
		}
		//插入相关元素
		oDd.appendChild(oFrag);
		oDl.innerHTML = aTmp.join("");
		oDl.appendChild(oDd);
		oContainer.className = "lin_calendar_container";
		oContainer.appendChild(oDl);
		//记录日历队列
		that.aCal.push(oContainer);
		//返回生成好的日历
		return oContainer
	},
	
	//创建日历
	create: function()
	{
		var that=this, 
		year = that.Year,
		month = that.Month,
		i = 0;
		that.container.className = "lin_calendar"; //※指定日历控件className
		//清空日历队列
		while(that.aCal[0]) that.container.removeChild(that.aCal.shift());
		//批量生成日历
		for(i = 0; i < that.count; i++) 
		{
			year += (month + (i ? 1 : 0)) > 12 ? 1 : 0;
			month = (month + (i ? 1 : 0)) % 12 || 12;
			that.container.appendChild(that._Draw(year, month))
		}
		//将日历插入页面, 如果未指定插入位置则插入BODY
		$(that.insertPosition).append(that.container);
		//日历生成完毕的回调方法
		that.onComplete();
		return this;
	},	
	
	//按钮设置(显示/隐藏)			
	setBtn:function(boolean)
	{
		var that=this;
		var $obj = $(that.container);
		//如果按钮没有创建过并且设置为显示, 则创建按钮, 并添加已创建标记
		if(!that.mark && boolean)
		{
			$obj.append(that.oClose);
			$obj.append(that.oPrev);
			$obj.append(that.oNext);
			//添加已创建标记
			that.mark = true
		}
		//如果按钮已经创建过, 则设置其显示/隐藏
		that.oClose.style.display = that.oPrev.style.display = that.oNext.style.display = boolean ? "block" : "none";			
	},
			
	//添加事件
	addEvent:function()
	{
		var that = this,
		obj = that.container,
		handler = null;
		handler = function(e)
		{
			e = e || event
			var oTarget = e.target || e.srcElement;
			switch(oTarget.className)
			{
				case "lin_calendar_close":
					that.hide();
				break;
				case "lin_calendar_prev":
					that.prevMonth();
				break;
				case "lin_calendar_next":
					that.nextMonth();
				break;
			}
			if(oTarget.tagName.toUpperCase() === "A" && oTarget.className != "disabled")
			{
				that.onSelectDay(oTarget);
			}
			if(oTarget.tagName.toUpperCase() === "SPAN" && oTarget.parentNode.tagName.toUpperCase() === "A")
			{
				that.onSelectDay(oTarget.parentNode);
			}
		}
		//为日历控件添加CLICK事件监听
		$(obj).on('click',handler);		
	},

	//格式化数字, 不足两位补0
	format:function(str)
	{
		return str.toString().replace(/^(\d)$/, "0$1")
	},
	
	//获取指定日期是星期几 @param date string yyyy-mm-dd
	getWeek:function(date)
	{
		var aWeek = ["\u65e5", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d"],
		arr = date.split(/-/g);
		return "\u661f\u671f" + aWeek[new Date(arr[0], arr[1] - 1, arr[2]).getDay()]
	},
	
	//节假日名字
	dayName: {
		"today":"\u4eca\u5929",
		"yuandan":"\u5143\u65e6",
		"chuxi":"\u9664\u5915",
		"chunjie":"\u6625\u8282",
		"yuanxiao":"\u5143\u5bb5\u8282",
		"qingming":"\u6e05\u660e",
		"wuyi":"\u52b3\u52a8\u8282",
		"duanwu":"\u7aef\u5348\u8282",
		"zhongqiu":"\u4e2d\u79cb\u8282",
		"guoqing":"\u56fd\u5e86\u8282"
	},
	
	//2012——2020年节假日数据
	Holidays:function()
	{
		return {
			today: [this.today],
			yuandan: ["2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01", "2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01"],
			chuxi: ["2012-01-22", "2013-02-09", "2014-01-30", "2015-02-18", "2016-02-07", "2017-01-27", "2018-02-15", "2019-02-04", "2020-01-24"],
			chunjie: ["2012-01-23", "2013-02-10", "2014-01-31", "2015-02-19", "2016-02-08", "2017-01-28", "2018-02-16", "2019-02-05", "2020-01-25"],
			yuanxiao: ["2012-02-06", "2013-02-24", "2014-2-14", "2015-03-05", "2016-02-22", "2017-02-11", "2018-03-02", "2019-02-19", "2020-02-8"],
			qingming: ["2012-04-04", "2013-04-04", "2014-04-05", "2015-04-05", "2016-04-04", "2017-04-04", "2018-04-05", "2019-04-05", "2020-04-04"],
			wuyi: ["2012-05-01", "2013-05-01", "2014-05-01", "2015-05-01", "2016-05-01", "2017-05-01", "2018-05-01", "2019-05-01", "2020-05-01"],
			duanwu: ["2012-06-23", "2013-06-12", "2014-06-02", "2015-06-20", "2016-06-09", "2017-05-30", "2018-06-18", "2019-06-07", "2020-06-25"],
			zhongqiu: ["2012-09-30", "2013-09-19", "2014-09-08", "2015-09-27", "2016-09-15", "2017-10-04", "2018-09-24", "2019-09-13", "2020-10-01"],
			guoqing: ["2012-10-01", "2013-10-01", "2014-10-01", "2015-10-01", "2016-10-01", "2017-10-01", "2018-10-01", "2019-10-01", "2020-10-01"]
		}
	},
	
	//计算是否有订房
	isHaveOrder:function(obj)
	{
		var that=this;
		if(new RegExp(obj["data-date"]).test(that.orders.join()))
		{
			obj.className ="order";
			obj["data-week"] ="已预订";
			obj["data-order"] ="1";
			obj.innerHTML = "<span>"+ obj.innerHTML +"</span>";
		}			
	},

	
	//计算是否为节假日
	isHoliday: function(obj, className)
	{
		var that=this;
		if(new RegExp(obj["data-date"]).test(that.Holidays()[className].join()))
		{
			obj.className = className;
			obj["data-week"] = that.dayName[className];
			obj.innerHTML = "<span>"+ obj.innerHTML +"</span>";
		}
	},	
	
	//根据日期创建日历
	draw:function(date)
	{
		var that=this;
		that.Year = date.getFullYear();
		that.Month = date.getMonth() + 1;
		that.create()
	},
	
	//当前月
	nowMonth:function()
	{
		var that=this;
		that.draw(new Date());
	},
	
	//下月
	nextMonth:function()
	{
		var that=this;
		that.draw(new Date(that.Year, that.Month + (that.count - 1), 1));
	},
	
	//上月
	prevMonth: function()
	{
		var that=this;
		that.draw(new Date(that.Year, that.Month - (that.count + 1), 1));
	},

	//显示日历
	show: function()
	{
		this.container.style.display = "block";
	},
	
	//隐藏日历
	hide: function() 
	{
		this.container.style.display = "none";
	}	
		
};   
